package com.example.calculator;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity {
	private TextView publicText;
	private String viewNumber = "";
	private int val1, val2, result;

	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		final TextView text = (TextView) findViewById(R.id.showText);
		publicText = text;

		final Button btn0 = (Button) findViewById(R.id.num0);
		final Button btn1 = (Button) findViewById(R.id.num1);
		final Button btn2 = (Button) findViewById(R.id.num2);
		final Button btn3 = (Button) findViewById(R.id.num3);
		final Button btn4 = (Button) findViewById(R.id.num4);
		final Button btn5 = (Button) findViewById(R.id.num5);
		final Button btn6 = (Button) findViewById(R.id.num6);
		final Button btn7 = (Button) findViewById(R.id.num7);
		final Button btn8 = (Button) findViewById(R.id.num8);
		final Button btn9 = (Button) findViewById(R.id.num9);
		final Button btnPlus = (Button) findViewById(R.id.plus);
		final Button btnEqual = (Button) findViewById(R.id.equal);

		btn0.setOnClickListener(new Handler(btn0));
		btn1.setOnClickListener(new Handler(btn1));
		btn2.setOnClickListener(new Handler(btn2));
		btn3.setOnClickListener(new Handler(btn3));
		btn4.setOnClickListener(new Handler(btn4));
		btn5.setOnClickListener(new Handler(btn5));
		btn6.setOnClickListener(new Handler(btn6));
		btn7.setOnClickListener(new Handler(btn7));
		btn8.setOnClickListener(new Handler(btn8));
		btn9.setOnClickListener(new Handler(btn9));

		btnPlus.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {

				if (viewNumber.equals("")) {
					return;
				}

				String str = (String) btnPlus.getText();
				publicText.setText(str);
				val1 = Integer.parseInt(viewNumber);
				viewNumber = "";
			}
		});

		btnEqual.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {

				if (viewNumber.equals("")) {
					return;
				}

				val2 = Integer.parseInt(viewNumber);
				result = val1 + val2;
				publicText.setText(result + "");
				viewNumber = "";
			}
		});

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	class Handler implements View.OnClickListener {
		Button btn = null;

		Handler(Button btn) {
			this.btn = btn;
		}

		@Override
		public void onClick(View v) {
			String str = (String) btn.getText();
			viewNumber += str;
			publicText.setText(viewNumber);
		}
	}
}
